package com.fhz.shop.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.fhz.shop.R;
import com.github.gzuliyujiang.imagepicker.ActivityBuilder;
import com.github.gzuliyujiang.imagepicker.CropImageView;
import com.github.gzuliyujiang.imagepicker.ImagePicker;
import com.github.gzuliyujiang.imagepicker.PickCallback;
import com.qiniu.android.common.FixedZone;
import com.qiniu.android.common.Zone;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.io.File;

/**
 * 时间:2023/8/25
 *
 * @author Mr.Feng
 * 简述: 图片选择
 */
public class ImagePickerActivity extends FragmentActivity {
    private Button mOpenCre;
    private Button mOpenGrll;




    private final PickCallback cropCallback = new PickCallback() {
        @Override
        public void onPermissionDenied(String[] permissions, String message) {
            Toast.makeText(ImagePickerActivity.this, message, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void cropConfig(ActivityBuilder builder) {
            builder.setMultiTouchEnabled(true)
                    .setGuidelines(CropImageView.Guidelines.ON_TOUCH)
                    .setCropShape(CropImageView.CropShape.OVAL)
                    .setRequestedSize(400, 400)
                    .setFixAspectRatio(true)
                    .setAspectRatio(1, 1);
        }

        @Override
        public void onCropImage(@Nullable Uri imageUri) {
            Toast.makeText(ImagePickerActivity.this, String.valueOf(imageUri), Toast.LENGTH_SHORT).show();
            System.out.println(String.valueOf(imageUri));
            String substring = String.valueOf(imageUri).substring(8);
            EventBus.getDefault().post(substring);
            finish();
//            UploadManager uploadManager = new UploadManager(config);
//            String data = String.valueOf(imageUri).substring(8);
//            String token = "<从服务端 SDK 获取>;";
//            uploadManager.put(data, key, token,
//                    new UpCompletionHandler() {
//                        @Override
//                        public void complete(String key, ResponseInfo info, JSONObject res) {
//                            //res 包含 hash、key 等信息，具体字段取决于上传策略的设置
//                            if(info.isOK()) {
//                                Log.i("qiniu", "Upload Success");
//                            } else {
//                                Log.i("qiniu", "Upload Fail");
//                                //如果失败，这里可以把 info 信息上报自己的服务器，便于后面分析上传错误原因
//                            }
//                            Log.i("qiniu", key + ",\r\n " + info + ",\r\n " + res);
//                        }
//                    }, null);
////
        }
    };

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picker_layout);
        mOpenCre = (Button) findViewById(R.id.open_cre);
        mOpenGrll = (Button) findViewById(R.id.open_grll);

        mOpenCre.setOnClickListener(v ->{
            onCamera();
        });

        mOpenGrll.setOnClickListener(v ->{
            onGallery();
        });

    }

    public void onCamera() {
        ImagePicker.getInstance().startCamera(this, true, cropCallback);
    }

    public void onGallery() {
        ImagePicker.getInstance().startGallery(this, true, cropCallback);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ImagePicker.getInstance().onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        ImagePicker.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    public static void launch(Context context){
        context.startActivity(new Intent(context,ImagePickerActivity.class));
    }

}
